//
//  BYDrawerViewController.m
//  仿QQ抽屉效果
//
//  Created by Sean Sebastian on 6/7/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "BYDrawerViewController.h"

@interface BYDrawerViewController ()

// 主控制器
@property (nonatomic , strong) UIViewController *mainViewController;
// 左边菜单控制器
@property (nonatomic , strong) UIViewController *leftViewController;
// 偏移值
@property (nonatomic , assign) CGFloat leftWidth;
// 遮盖按钮
@property (nonatomic , weak) UIButton *coverBtn;
// 新的跳转控制器
@property (nonatomic , strong) UIViewController *destViewController;

@end

@implementation BYDrawerViewController

// 返回UIWindow的rootViewController
+ (instancetype)sharedDrawerViewController
{
    return (BYDrawerViewController *)[[UIApplication sharedApplication].keyWindow rootViewController];
}

// 快速创建一个抽屉控制器并返回
+ (instancetype)drawerViewControllerWithMainController:(UIViewController *)mainController andLeftController:(UIViewController *)leftViewController andLeftWidth:(CGFloat)leftWidth
{
    BYDrawerViewController *drawerViewController = [[BYDrawerViewController alloc] init];
    // 保存属性
    drawerViewController.mainViewController = mainController;
    drawerViewController.leftViewController = leftViewController;
    drawerViewController.leftWidth = leftWidth;
    
    // 添加view
    [drawerViewController.view addSubview:leftViewController.view];
    [drawerViewController.view addSubview:mainController.view];
    // 控制器间建立父子关系
    [drawerViewController addChildViewController:leftViewController];
    [drawerViewController addChildViewController:mainController];
    
    return drawerViewController;
}

// 打开左边菜单控制器
- (void)leftOpen
{
    // 动画形式，options是执行的方式
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.mainViewController.view.transform = CGAffineTransformMakeTranslation(self.leftWidth, 0);
        self.leftViewController.view.transform = CGAffineTransformIdentity;     // 回到初始状态
    } completion:^(BOOL finished) {
        // 添加遮盖按钮
        [self.mainViewController.view addSubview:self.coverBtn];
        
    }];
}

// 跳转控制器
- (void)switchViewController:(UIViewController *)vc
{
    // 保存属性
    self.destViewController = vc;
    
    vc.view.frame = self.mainViewController.view.bounds;
    vc.view.transform = self.mainViewController.view.transform;
    
    [self.view addSubview:vc.view];
    [self addChildViewController:vc];
    
    // 以动画形式让控制器回到最初状态
    [UIView animateWithDuration:0.25 animations:^{
        vc.view.transform = CGAffineTransformIdentity;
        [self coverBtnClick];
    }];
}

// 返回
- (void)backHome
{
    [UIView animateWithDuration:0.25 animations:^{
        self.destViewController.view.transform = CGAffineTransformMakeTranslation(self.view.bounds.size.width, 0);
    } completion:^(BOOL finished) {
        [self.destViewController.view removeFromSuperview];
        [self.destViewController removeFromParentViewController];
        self.destViewController = nil;
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.leftViewController.view.transform = CGAffineTransformMakeTranslation( -self.leftWidth , 0);
    
    // 设置阴影
    self.mainViewController.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.mainViewController.view.layer.shadowOffset = CGSizeMake(-3, -3);
    self.mainViewController.view.layer.shadowOpacity = 0.2;
    self.mainViewController.view.layer.shadowRadius = 5;
    
    // 获取tabbarController的子控制器
    for (UIViewController *vc in self.mainViewController.childViewControllers) {
        // 创建边缘拖拽手势识别对象
        UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(rightDrag:)];
        // 设置拖拽方向
        pan.edges = UIRectEdgeLeft;
        // 添加拖拽手势
        [vc.view addGestureRecognizer:pan];
        
    }
}

// 拖拽手势的回调方法
- (void)rightDrag:(UIScreenEdgePanGestureRecognizer *)pan
{
    // 获取点
    CGPoint point = [pan translationInView:pan.view];
    // 获取屏幕宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;

    // 判断状态，当手势结束和手势取消的时候
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        
        // 判断主主控制器的view的x有没有超过屏幕的一半
        if (self.mainViewController.view.frame.origin.x > screenW * 0.5) {
            [self leftOpen];
        }else{
            [self coverBtnClick];
        }
        
        
    // 判断状态，手势改变的时候
    }else if (pan.state == UIGestureRecognizerStateChanged){
        self.mainViewController.view.transform = CGAffineTransformMakeTranslation(point.x, 0);
        self.leftViewController.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth + point.x, 0);
    }
}

// 懒加载
- (UIButton *)coverBtn
{
    if (_coverBtn == nil) {
        UIButton *btn = [[UIButton alloc] initWithFrame:self.mainViewController.view.bounds];
        btn.backgroundColor = [UIColor clearColor];
        [btn addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        // 创建普通拖拽手势识别对象
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(btnDraw:)];
        [btn addGestureRecognizer:pan];
        _coverBtn = btn;
        return btn;
    }
    return _coverBtn;
}

// 拖拽手势的回调方法
- (void)btnDraw:(UIPanGestureRecognizer *)pan
{
    // 获取点
    CGPoint point = [pan translationInView:pan.view];
    // 获取屏幕宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    if (point.x > 0) {
        return;
    }
    
    CGFloat dis = self.leftWidth - ABS(point.x);
    
    // 判断状态，当手势结束和手势取消的时候
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        
        // 判断主主控制器的view的x有没有超过屏幕的一半
        if (self.mainViewController.view.frame.origin.x > screenW * 0.5) {
            [self leftOpen];
        }else{
            [self coverBtnClick];
        }
        
        // 判断状态，手势改变的时候
    }else if (pan.state == UIGestureRecognizerStateChanged){
        self.mainViewController.view.transform = CGAffineTransformMakeTranslation(MAX(0, dis), 0);
        self.leftViewController.view.transform = CGAffineTransformMakeTranslation(-self.leftWidth + dis, 0);
    }
}

// 遮盖按钮点击事件
- (void)coverBtnClick
{
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.mainViewController.view.transform = CGAffineTransformIdentity;
        self.leftViewController.view.transform = CGAffineTransformMakeTranslation( -self.leftWidth , 0);
    } completion:^(BOOL finished) {
        // 去除遮盖按钮
        [self.coverBtn removeFromSuperview];
        self.coverBtn = nil;
    }];
}

@end
